package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import java.text.ChoiceFormat;
import java.text.DecimalFormat;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.soap.MessageFactory;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JTextField;
import javax.swing.JLabel;

import org.eclipse.wb.swing.FocusTraversalOnArray;

import java.awt.Component;

import javax.swing.border.LineBorder;

import java.awt.Color;
import javax.swing.JTabbedPane;
import javax.swing.JLayeredPane;

public class Format extends JFrame {

	private JPanel contentPane;
	private JButton btnNewButton;
	private JButton btnNewButton_3;
	private JButton btnNewButton_2;
	private JButton btnNewButton_4;
	private JButton btnNewButton_1;
	private JScrollPane scrollPane;
	private JTextArea result;
	private JTextField money;
	private JLabel label;
	private JLabel label_1;
	private JLabel gradeLabel;
	private JTextField irum;
	private JTextField grade;
	private JLabel lblNewLabel_1;
	private JLabel label_2;
	private JTextField moneyfloat;
	private JLabel lblA;
	private JLabel lblB;
	private JLabel lblC;
	private JLabel lblD;
	private JTextField acut;
	private JTextField bcut;
	private JTextField ccut;
	private JTextField dcut;
	private JLabel lblKor;
	private JLabel lblEng;
	private JLabel lblMath;
	private JLabel lblComp;
	private JTextField korscore;
	private JTextField engscore;
	private JTextField mathscore;
	private JTextField compscore;
	private JTextField dformat;
	private JTextField birth;
	private JTextField addr;
	private JLabel label_3;
	private JLabel label_4;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Format frame = new Format();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Format() {
		setTitle("Format Example Program");
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 639, 588);
		contentPane = new JPanel();
		contentPane.setBackground(Color.PINK);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getBtnNewButton());
		contentPane.add(getBtnNewButton_1());
		contentPane.add(getBtnNewButton_2());
		contentPane.add(getBtnNewButton_3());
		contentPane.add(getBtnNewButton_4());
		contentPane.add(getScrollPane());
		contentPane.add(getMoney());
		contentPane.add(getLabel());
		contentPane.add(getLabel_1());
		contentPane.add(getGradeLabel());
		contentPane.add(getIrum());
		contentPane.add(getGrade());
		contentPane.add(getLabel_2());
		contentPane.add(getMoneyfloat());
		contentPane.add(getLblA());
		contentPane.add(getLblB());
		contentPane.add(getLblC());
		contentPane.add(getLblD());
		contentPane.add(getAcut());
		contentPane.add(getBcut());
		contentPane.add(getCcut());
		contentPane.add(getDcut());
		contentPane.add(getLblKor());
		contentPane.add(getLblEng());
		contentPane.add(getLblMath());
		contentPane.add(getLblComp());
		contentPane.add(getKorscore());
		contentPane.add(getEngscore());
		contentPane.add(getMathscore());
		contentPane.add(getCompscore());
		contentPane.add(getDformat());
		contentPane.setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{getMoney(), getIrum(), getGrade(), getBtnNewButton(), getBtnNewButton_3(), getBtnNewButton_2(), getBtnNewButton_4(), getBtnNewButton_1(), getScrollPane(), getResult(), getLabel(), getLabel_1(), getGradeLabel()}));
		contentPane.add(getBirth());
		contentPane.add(getAddr());
		contentPane.add(getLabel_3());
		contentPane.add(getLabel_4());
		dformat.setText("#,###,###.##");
		acut.setText("90");
		bcut.setText("80");
		ccut.setText("70");
		dcut.setText("60");
		birth.setText("1234.12.12");
		money.setText("10000");
		moneyfloat.setText("1234567.89");
		contentPane.add(getLblNewLabel_1());
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("StringFormat");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					int k = Integer.parseInt(money.getText());
					float kk = Float.parseFloat(moneyfloat.getText());
					char gra = grade.getText().charAt(0);					
					String str1 = String.format("입금 : %d원", k);
					String str2 = String.format("입금 : %10d원", k);
					String str3 = String.format("입금 : %-10d원", k);									
					String f1 = String.format("입금: %10.1f원, 이자: %10.3f원", kk, kk);					
					String str4 = String.format("성명:%s --> 학점:%c", irum.getText(), gra);
					result.append(str1+"\n");
					result.append(str2+"\n");
					result.append(str3+"\n");
					result.append(f1+"\n");
					result.append(str4+"\n");
				}
			});
			btnNewButton.setBounds(95, 157, 123, 23);
		}
		return btnNewButton;
	}
	
	private JButton getBtnNewButton_2() {
		if (btnNewButton_2 == null) {
			btnNewButton_2 = new JButton("DecimalFormat");
			btnNewButton_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					DecimalFormat df = new DecimalFormat(dformat.getText());
					String str = df.format(1234567.89);
					result.append(str+"\n");
				}
			});
			btnNewButton_2.setBounds(95, 223, 123, 23);
		}
		return btnNewButton_2;
	}
	private JButton getBtnNewButton_3() {
		if (btnNewButton_3 == null) {
			btnNewButton_3 = new JButton("ChoiceFormat");
			btnNewButton_3.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					double limit[] = {0,Double.parseDouble(dcut.getText()),
							Double.parseDouble(ccut.getText()),
							Double.parseDouble(bcut.getText()),
							Double.parseDouble(acut.getText())};
					String pattern="0<F|"+dcut.getText()+"<D|"+ccut.getText()+"<C|"+bcut.getText()+"<B|"+acut.getText()+"<A"; // 경계값은 포함시키지 않음.
					String grade[]={"F","D","C","B","A"};
					int score[]={Integer.parseInt(korscore.getText()),
							Integer.parseInt(engscore.getText()),
							Integer.parseInt(mathscore.getText()),
							Integer.parseInt(compscore.getText())};
					
					ChoiceFormat cf = new ChoiceFormat(limit, grade);
					ChoiceFormat cf2 = new ChoiceFormat(pattern);
					for(int i : score){
						String str = String.format("%4d -->%s",i,cf.format(i));
						result.append(str);
					}
					result.append("\n");
					for(int i : score){
						String str = String.format("%4d -->%s",i,cf2.format(i));
						result.append(str);
					}
					result.append("\n");
				}
			});
			btnNewButton_3.setBounds(246, 157, 133, 23);
		}
		return btnNewButton_3;
	}
	private JButton getBtnNewButton_4() {
		if (btnNewButton_4 == null) {
			btnNewButton_4 = new JButton("MessageFormat");
			btnNewButton_4.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					String str = "성명:{0},생년월일:{1},주소{2}";
					String data[][] = {{irum.getText(),birth.getText(),addr.getText()},
							{irum.getText(),birth.getText(),addr.getText()},
							{irum.getText(),birth.getText(),addr.getText()}
					};
					
					for(String s[] : data){
						String r = MessageFormat.format(str, s);
						result.append(r+"\n");
					}
					
				}
			});
			btnNewButton_4.setBounds(455, 90, 140, 23);
		}
		return btnNewButton_4;
	}
	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("SimpleDateFormat");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					SimpleDateFormat sd = new SimpleDateFormat("yyyy년 MM월 dd일(E요일)");
					SimpleDateFormat sd2 = new SimpleDateFormat("hh:mm:ss a");
					String str = sd.format(new Date());
					String str1 = sd2.format(new Date());
					
					result.append(str+"\n");
					result.append(str1+"\n");
				}
			});
			btnNewButton_1.setBounds(246, 190, 135, 23);
		}
		return btnNewButton_1;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBorder(new LineBorder(Color.ORANGE));
			scrollPane.setBounds(12, 273, 600, 267);
			scrollPane.setViewportView(getResult());
		}
		return scrollPane;
	}
	private JTextArea getResult() {
		if (result == null) {
			result = new JTextArea();
		}
		return result;
	}	
	
	private JTextField getMoney() {
		if (money == null) {
			money = new JTextField();
			money.setBounds(96, 25, 123, 23);
			money.setColumns(10);
		}
		return money;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("입금액(정수)");
			label.setBounds(20, 25, 97, 23);
		}
		return label;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("이름");
			label_1.setBounds(20, 91, 97, 23);
		}
		return label_1;
	}
	private JLabel getGradeLabel() {
		if (gradeLabel == null) {
			gradeLabel = new JLabel("학점");
			gradeLabel.setBounds(20, 124, 97, 23);
		}
		return gradeLabel;
	}
	private JTextField getIrum() {
		if (irum == null) {
			irum = new JTextField();
			irum.setColumns(10);
			irum.setBounds(96, 91, 123, 23);
		}
		return irum;
	}
	private JTextField getGrade() {
		if (grade == null) {
			grade = new JTextField();
			grade.setColumns(10);
			grade.setBounds(96, 124, 123, 23);
		}
		return grade;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("");
			lblNewLabel_1.setBorder(new LineBorder(Color.ORANGE));
			lblNewLabel_1.setBounds(12, 10, 600, 253);
		}
		return lblNewLabel_1;
	}
	private JLabel getLabel_2() {
		if (label_2 == null) {
			label_2 = new JLabel("입금액(실수)");
			label_2.setBounds(20, 58, 97, 23);
		}
		return label_2;
	}
	private JTextField getMoneyfloat() {
		if (moneyfloat == null) {
			moneyfloat = new JTextField();
			moneyfloat.setColumns(10);
			moneyfloat.setBounds(96, 58, 123, 23);
		}
		return moneyfloat;
	}
	private JLabel getLblA() {
		if (lblA == null) {
			lblA = new JLabel("A");
			lblA.setBounds(231, 25, 23, 23);
		}
		return lblA;
	}
	private JLabel getLblB() {
		if (lblB == null) {
			lblB = new JLabel("B");
			lblB.setBounds(231, 58, 23, 23);
		}
		return lblB;
	}
	private JLabel getLblC() {
		if (lblC == null) {
			lblC = new JLabel("C");
			lblC.setBounds(231, 91, 23, 23);
		}
		return lblC;
	}
	private JLabel getLblD() {
		if (lblD == null) {
			lblD = new JLabel("D");
			lblD.setBounds(231, 124, 23, 23);
		}
		return lblD;
	}
	private JTextField getAcut() {
		if (acut == null) {
			acut = new JTextField();
			acut.setBounds(247, 26, 38, 23);
			acut.setColumns(10);
		}
		return acut;
	}
	private JTextField getBcut() {
		if (bcut == null) {
			bcut = new JTextField();
			bcut.setColumns(10);
			bcut.setBounds(247, 59, 38, 23);
		}
		return bcut;
	}
	private JTextField getCcut() {
		if (ccut == null) {
			ccut = new JTextField();
			ccut.setColumns(10);
			ccut.setBounds(247, 92, 38, 23);
		}
		return ccut;
	}
	private JTextField getDcut() {
		if (dcut == null) {
			dcut = new JTextField();
			dcut.setColumns(10);
			dcut.setBounds(247, 124, 38, 23);
		}
		return dcut;
	}
	private JLabel getLblKor() {
		if (lblKor == null) {
			lblKor = new JLabel("Kor");
			lblKor.setBounds(291, 25, 38, 23);
		}
		return lblKor;
	}
	private JLabel getLblEng() {
		if (lblEng == null) {
			lblEng = new JLabel("Eng");
			lblEng.setBounds(291, 58, 38, 23);
		}
		return lblEng;
	}
	private JLabel getLblMath() {
		if (lblMath == null) {
			lblMath = new JLabel("Math");
			lblMath.setBounds(291, 91, 38, 23);
		}
		return lblMath;
	}
	private JLabel getLblComp() {
		if (lblComp == null) {
			lblComp = new JLabel("Comp");
			lblComp.setBounds(291, 124, 38, 23);
		}
		return lblComp;
	}
	private JTextField getKorscore() {
		if (korscore == null) {
			korscore = new JTextField();
			korscore.setColumns(10);
			korscore.setBounds(341, 26, 38, 23);
		}
		return korscore;
	}
	private JTextField getEngscore() {
		if (engscore == null) {
			engscore = new JTextField();
			engscore.setColumns(10);
			engscore.setBounds(341, 59, 38, 23);
		}
		return engscore;
	}
	private JTextField getMathscore() {
		if (mathscore == null) {
			mathscore = new JTextField();
			mathscore.setColumns(10);
			mathscore.setBounds(341, 92, 38, 23);
		}
		return mathscore;
	}
	private JTextField getCompscore() {
		if (compscore == null) {
			compscore = new JTextField();
			compscore.setColumns(10);
			compscore.setBounds(341, 124, 38, 23);
		}
		return compscore;
	}
	private JTextField getDformat() {
		if (dformat == null) {
			dformat = new JTextField();
			dformat.setColumns(10);
			dformat.setBounds(95, 190, 123, 23);
		}
		return dformat;
	}
	private JTextField getBirth() {
		if (birth == null) {
			birth = new JTextField();
			birth.setColumns(10);
			birth.setBounds(455, 25, 140, 23);
		}
		return birth;
	}
	private JTextField getAddr() {
		if (addr == null) {
			addr = new JTextField();
			addr.setColumns(10);
			addr.setBounds(455, 58, 140, 23);
		}
		return addr;
	}
	private JLabel getLabel_3() {
		if (label_3 == null) {
			label_3 = new JLabel("생년월일");
			label_3.setBounds(385, 25, 58, 23);
		}
		return label_3;
	}
	private JLabel getLabel_4() {
		if (label_4 == null) {
			label_4 = new JLabel("주소");
			label_4.setBounds(385, 58, 58, 23);
		}
		return label_4;
	}
}
